/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file nodeIconManager.h
  \author Cory Dixon
*/

#ifndef __DM_LAYER_MANAGER_H
#define __DM_LAYER_MANAGER_H

#include "LayerManager.h"

/*<-------Defines--------->*/
#define MAX_PREV_LINE 3
/*<------End Defines------>*/

typedef struct _DMLineAlloc_t {
	double x; double y; //coordinates of group, evertying else is reletive to the group
	GooCanvasItem *pointG;
	GooCanvasItem *point;
	GooCanvasItem *text;
	GooCanvasItem *text_p;
	GooCanvasItem *text_t;
	GooCanvasItem *orbit;
	GooCanvasItem *line_prev[MAX_PREV_LINE];
	GooCanvasItem *line_next;

	char  distPrev_txt[32];
	char  distTotal_txt[32];
	float distPrev;
	float distTotal;

	_DMLineAlloc_t() { 
		pointG = point = text = orbit = line_next = NULL; 
		for(int i=0; i < MAX_PREV_LINE; i++) line_prev[i] = NULL;
	}
} DMLineAlloc_t;

typedef struct _DistMeasAlloc {
	uint8_t         last_wp;
	uint8_t         current_wp;
	GooCanvasItem  *wp_group;

	GPSWaypointPlan   plan;
	WPMask_t          editing_mask;

	DMLineAlloc_t imgPoints[NUM_WP_POINTS];

	_DistMeasAlloc() { wp_group = NULL; }

} DistMeasAlloc;


class DMManager : public LayerManager
{
	protected:
		// GPS flight plans
		DistMeasAlloc dmLines;

		double pointWidth;
		double lineWidth;

		double mouse_x;
		double mouse_y;
		int    mouse_fp_ind;
		int    mouse_wp_ind;
		double last_theta;

		int    drag_point;    // true if dragging an individual point
		bool   drag_plan;     // true if dragging plan
		bool   rotate_plan;   // true if rotating plan
		bool   dragging;      // true if dragging a plan related item
		bool   creating_plan; // true if creating a local plan

		void translate_point(int pointInd, double dX, double dY);
		void rotate_point(int pointInd, double dTheta);

	public:
		DMManager();
		~DMManager();

		virtual void init(GPSMap *map = NULL);
		virtual void updateMap();
		virtual void translate_from_zoomChange(double zoom_factor);

		// functions for the flight plan layer
		void setup_item(GooCanvasItem *item);

		bool add_waypoint(GPSWaypoint_t &point) {
			return dmLines.plan.add_waypoint(point);
		}
		int numPoints() {
			return dmLines.plan.numPoints();
		}
		GPSWaypoint_t get_waypoint( uint8_t ind) {
			return dmLines.plan.get_waypoint(ind);
		}
		bool set_waypoint(GPSWaypoint_t &point) {
			return dmLines.plan.set_waypoint(point);
		}
		void setPlanEditing(bool val) {
			creating_plan = val;
		}

		void create_canvas_flightplan();
		void clear_client();
		void clear_client_canvas();

		static bool button_press_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, 
				GdkEventButton *event, DMManager *me);
		static bool button_release_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, 
				GdkEventButton *event, DMManager *me);
		static bool motion_notify_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, 
				GdkEventMotion *event, DMManager *me);

};

#endif
